﻿using Cxuu.Common;
using Cxuu.Models;
using Microsoft.Extensions.DependencyInjection;
using PetaPoco;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Cxuu.Repository
{
    [UseDI(ServiceLifetime.Scoped)]
    public class ExtVideoRepository : BaseRepository<ExtVideo, PetaPocoContext>
    {

        public ExtVideoRepository(PetaPocoContext petaPocoContext) : base(petaPocoContext) { }


        /// <summary>
        /// 前台访问时 浏览量+1
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task IncHits(int? id)
        {
            Sql sql = Sql.Builder;
            sql.Append("SET Hits = Hits + 1")
                .Where("Id =@0", id);
            await CreateDAO().UpdateAsync<ExtVideo>(sql);
        }


        /// <summary>
        /// 前台查询一条
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ViewExtVideoCateAndContent> GetOneAsync(int? id)
        {
            Sql sql = Sql.Builder;
            sql.Select("*")
            .From("cxuu_ext_video")
            .Where("Id =@0", id)
            .Where("Status = 1");
            return await CreateDAO().FirstOrDefaultAsync<ViewExtVideoCateAndContent>(sql);
        }


        /// <summary>
        /// 根据CID 查询一条 用于判断当前栏目下有没有数据
        /// </summary>
        /// <param name="cid"></param>
        /// <returns></returns>
        public async Task<ExtVideo> GetByCidAsync(int cid)
        {
            Sql sql = Sql.Builder;
            sql.Select("*").From("cxuu_ext_video").Where("Cid =@0", cid);
            return await CreateDAO().FirstOrDefaultAsync<ExtVideo>(sql);
        }

        /// <summary>
        /// 获取视频及栏目列表  后台使用
        /// </summary>
        /// <param name="cid"></param>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="orderBy"></param>
        /// <param name="keyword"></param>
        /// <returns></returns>
        public async Task<Page<ViewExtVideoCateAndContent>> GetListAsync(int? cid, int page, int limit, string orderBy, string keyword, int status, int uid = 0)
        {
            Sql sql = Sql.Builder;
            sql.Select("video.Id,cate.Name as CateName,g.Groupname,u.Nickname,video.Cid,video.Title,video.Examine,video.Hits,video.Time,video.Status,video.AttA,video.AttB,video.AttC")
                .From("cxuu_ext_video as video")
                .LeftJoin("cxuu_ext_video_cate as cate").On("video.Cid = cate.Id");

            sql.LeftJoin("cxuu_admin_user u").On("video.Uid=u.Id");
            sql.LeftJoin("cxuu_admin_group g").On("u.Gid=g.Id");

            if (status != 2)
                sql.Where("video.Status = @0", status);

            if (uid != 0)
                sql.Where("video.Uid = @0", uid);

            if (cid > 0)
                sql.Where("video.Cid =@0", cid);

            if (!string.IsNullOrEmpty(keyword))
                sql.Where("video.Title like @0", "%" + keyword + "%");

            if (!string.IsNullOrEmpty(orderBy))
                sql.OrderBy(orderBy);

            return await CreateDAO().PageAsync<ViewExtVideoCateAndContent>(page, limit, sql);
        }

        /// <summary>
        /// 获取视频及栏目列表条数列表 前台使用
        /// </summary>
        /// <param name="cid"></param>
        /// <param name="limit"></param>
        /// <param name="orderBy"></param>
        /// <returns></returns>
        public async Task<List<ViewExtVideoCateAndContent>> GetListByLimtAsync(string cid, int limit, int att, string orderBy)
        {
            Sql sql = Sql.Builder;
            sql.Select("video.Id,cate.Name as CateName,video.Cid,video.Title,video.Examine,video.Img,video.Hits,video.Time,video.AttA,video.AttB,video.AttC")
                .From("cxuu_article as video")
                .LeftJoin("cxuu_ext_video_cate as cate")
                .On("video.Cid = cate.Id")
                .Where("video.Status = 1");
            //属性
            if (att == 1)
                sql.Where("video.AttA = 1");

            if (att == 2)
                sql.Where("video.AttB = 1");

            if (att == 3)
                sql.Where("video.AttC = 1");

            if (cid != "0")
                sql.Where("video.Cid IN (" + cid + ")");

            if (!string.IsNullOrEmpty(orderBy))
                sql.OrderBy(orderBy);

            sql.Append("limit @0", limit);
            return await CreateDAO().FetchAsync<ViewExtVideoCateAndContent>(sql);
        }
        /// <summary>
        /// 前台搜索用
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="keyword"></param>
        /// <returns></returns>
        public async Task<Page<ViewExtVideoCateAndContent>> GetListAsync(int page, int limit, string keyword)
        {
            Sql sql = Sql.Builder;
            sql.Select("A.Id,B.Name as CateName,A.Cid,A.Title,A.Examine,A.Time,A.Status")
                .From("cxuu_ext_video as A")
                .LeftJoin("cxuu_ext_video_cate as B").On("A.Cid = B.Id");

            sql.Where("A.Status = 1");

            if (!string.IsNullOrEmpty(keyword))
            {
                sql.Where("A.Title like @0", "%" + keyword + "%");
            }

            sql.OrderBy("A.Id");

            return await CreateDAO().PageAsync<ViewExtVideoCateAndContent>(page, limit, sql);
        }

        public async Task<int> UpdateAttAsync(int id, int status, string att)
        {
            Sql sql = Sql.Builder;
            switch (att)
            {
                case "AttA":
                    sql.Append("SET AttA = @0 WHERE Id=@1", status, id);
                    break;
                case "AttB":
                    sql.Append("SET AttB = @0 WHERE Id=@1", status, id);
                    break;
                case "AttC":
                    sql.Append("SET AttC = @0 WHERE Id=@1", status, id);
                    break;
                default:
                    sql.Append("SET Status = @0 WHERE Id=@1", status, id);
                    break;
            }
            return await CreateDAO().UpdateAsync<ExtVideo>(sql);
        }
    }
}
